/**
 * popup window
 * v 0.04  - 08/11/2010
 */
function TrDialog(){
    this.title = "Dialog";
    this.body="hello";
    this.width=200;
    this.height=200;
    this.win=self;
    /**
     * function: init()
     * @param: json object: {width: width of popup,height: height of popup,resize: true or false , model: true or false}
     */
    this.init=function(arg)
    {
        _root=this;
        var sizep=_root.getSizePage();
        var coord=_root.GetCoords();
        this.width=sizep.w/2;
        this.height=sizep.h/2;
        if(arg!=undefined)
        {
            if(arg.width!=undefined)
            {
                this.width=arg.width;
            }
            if(arg.height!=undefined)
            {
                this.height=arg.height;
            }
        }
        this.divall     = document.createElement("div");
        this.divmask    = document.createElement("div");
        this.divDialog  = document.createElement("div");
        this.divhead    = document.createElement("div");
        this.divbody    = document.createElement("div");
        this.divunderlay= document.createElement("div");
        this.divclose   = document.createElement("div");
        //this.divmove    = document.createElement("div");
        this.divresizebr= document.createElement("div");
        this.divresizetl= document.createElement("div");
        this.divresizetr= document.createElement("div");
        this.divresizebl= document.createElement("div");
        this.divresizett= document.createElement("div");
        this.divresizerr= document.createElement("div");
        this.divresizebb= document.createElement("div");
        this.divresizell= document.createElement("div");

        this.divmask.className      = "mask";
        this.divDialog.className    = "Dialog";
        this.divhead.className      = "tt";
        this.divbody.className      = "body";
        this.divunderlay.className  = "underlay";
        this.divclose.className     = "cl";
        //this.divmove.className      = "divmove";
        this.divresizebr.className  = 'resize';
        this.divresizetl.className  = 'resize resizetl';
        this.divresizetr.className  = 'resize resizetr';
        this.divresizebl.className  = 'resize resizetr';
        this.divresizett.className  = 'resize resizett';
        this.divresizerr.className  = 'resize resizell';
        this.divresizebb.className  = 'resize resizett';
        this.divresizell.className  = 'resize resizell';
        this.divall.style.display   = 'none';
        //this.divmove.style.display='none';

        if(arg && arg.model==true)
        {
            this.divall.appendChild(this.divmask);
        }
        this.divall.appendChild(this.divunderlay);
        if(arg.resize==true)
        {
            this.divDialog.appendChild(this.divresizebr);
            this.divDialog.appendChild(this.divresizetl);
            this.divDialog.appendChild(this.divresizetr);
            this.divDialog.appendChild(this.divresizebl);
            this.divDialog.appendChild(this.divresizett);
            this.divDialog.appendChild(this.divresizerr);
            this.divDialog.appendChild(this.divresizebb);
            this.divDialog.appendChild(this.divresizell);
        }

        this.divDialog.appendChild(this.divhead);
        this.divDialog.appendChild(this.divbody);
        this.divDialog.appendChild(this.divclose);

        this.divall.appendChild(this.divDialog);

        //this.divall.appendChild(this.divmove);
        this.win.document.body.appendChild(this.divall);

        this.divhead.onselectstart=function(){return false};
        this.moveTo(coord.x+sizep.w/2-this.width/2,coord.y+sizep.h/2-this.height/2-50);

    };
    this.setTitle=function (title)
    {
        this.title=title;
    };
    this.setBody=function (body)
    {
        this.body=body;
    };
    this.setUrl=function(url)
    {
        this.body="<iframe src='"+url+"' style='width:100%; height:100%; border:0px'></iframe>";
    };
    this.moveTo=function(x,y)
    {
        if(y<0)y=0;
        this.setPosition(this.divunderlay,x-5,y+5);
        this.setPosition(this.divDialog,x,y);
        //this.setPosition(this.divmove,x,y);
    };
    this.resizeTo = function(w,h)
    {
        this.width=w;
        this.height=h;
        this.setSize(this.divbody,w,h);
        this.setSize(this.divunderlay,w+10,h+33);
        this.setSize(this.divDialog,w,h+31);
        //this.setSize(this.divmove,w,h+30);
        this.setSize(this.divresizett,w-16,12);
        this.setSize(this.divresizebb,w-16,12);
        this.setSize(this.divresizell,12,h);
        this.setSize(this.divresizerr,12,h);
        this.setPosition(this.divresizebr,w-8,h+22);
        this.setPosition(this.divresizetr,w-8,-8);
        this.setPosition(this.divresizebl,-8,h+22);
        this.setPosition(this.divresizebb,8,h+30);
        this.setPosition(this.divresizerr,w-8,10);
    };
    this.setSize = function(obj,w,h)
    {
        obj.style.width=w;
        obj.style.height=h;
        obj.width=w;
        obj.height=h;
    };
    this.setPosition = function(obj,x,y)
    {
        obj.style.left=x;
        obj.style.top=y;
        obj.left=x;
        obj.top=y;
    };
    this.setCursor=function(str)
    {
        //this.divmove.style.cursor=str;
        this.divmask.style.cursor=str;
    };
    this.show=function(x,y)
    {
        var _root=this;
        var sizep=_root.getSizePage();
        var coord=_root.GetCoords();
        _root.divhead.innerHTML=_root.title;
        _root.divbody.innerHTML=_root.body;
        _root.divall.style.display='';
        _root.win.document.body.className='masked';
        _root.resizeTo(_root.width,_root.height);
        var sizeps=_root.getPageSizeWithScroll();
        if(sizeps.y>sizep.h)sizeps.x-=17;
        _root.setSize(_root.divmask,sizeps.x,sizeps.y);
        if(x!=undefined && y!=undefined)
        {
            this.moveTo(x,y);
        }else
        {
           this.moveTo(coord.x+sizep.w/2-this.width/2,coord.y+sizep.h/2-this.height/2-50);
        }

        try{
            this.omm=this.win.document.onmousemove;
            this.omu=this.win.document.onmouseup;
            this.wors=this.win.window.onresize;
            this.wosc=this.win.window.onscroll;
        }catch(e){};
        this.divclose.onclick=function()
        {
            _root.close();
        };

        this.divhead.onmousedown=function(e)
        {
            if (!e) e = this.win.window.event;
            var coord=_root.GetCoords();
            _root.isMove=true;
            _root.dragx=e.clientX-_root.divDialog.left+coord.x;
            _root.dragy=e.clientY-_root.divDialog.top+coord.y;
            //_root.divmove.style.cursor="move";
        };
        this.divresizetl.onmousedown=function(e)
        {
            _root.resizetype=1;
            _root.setCursor("se-resize");
        };
        this.divresizetr.onmousedown=function(e)
        {
            _root.resizetype=2;
            _root.setCursor("sw-resize");
        };
        this.divresizebr.onmousedown=function(e)
        {
            _root.resizetype=3;
            _root.setCursor("se-resize");
        };
        this.divresizebl.onmousedown=function(e)
        {
            _root.resizetype=4;
            _root.setCursor("sw-resize");

        };
        this.divresizett.onmousedown=function(e)
        {
            _root.resizetype=5;
            _root.setCursor("n-resize");
        };
        this.divresizerr.onmousedown=function(e)
        {
            _root.resizetype=6;
            _root.setCursor("e-resize");
        };
        this.divresizebb.onmousedown=function(e)
        {
            _root.resizetype=7;
            _root.setCursor("n-resize");
        };
        this.divresizell.onmousedown=function(e)
        {
            _root.resizetype=8;
            _root.setCursor("e-resize");
        };
        this.win.document.onmousemove=function(e)
        {
            if (!e) e = this.win.window.event;

            if(_root.isMove==true)
            {
                var coord=_root.GetCoords();
                var sizep=_root.getSizePage();
                var x= e.clientX +coord.x;
                var y=e.clientY+coord.y;
                var maxleft=coord.x+sizep.w-10;
                var maxtop=coord.y+sizep.h-10;

                if(x>maxleft)x=maxleft;
                if(x<5)x=5;
                if(y>maxtop)y=maxtop;
                if(y<5)y=5;
                //_root.setPosition(_root.divmove,x-_root.dragx,y-_root.dragy);
                //_root.divmove.style.display='';
                _root.moveTo(x-_root.dragx,y-_root.dragy);
            }else if(_root.resizetype>0)
            {
                var coord=_root.GetCoords();
                var sizeps=_root.getPageSizeWithScroll();
                var mx= e.clientX +coord.x;
                var my=e.clientY+coord.y;
                if(mx<coord.x)mx=coord.x;
                if(mx>sizeps.x-10)mx=sizeps.x-10;
                if(my<coord.y)my=coord.y;
                if(my>sizeps.y-15)my=sizeps.y-15;
                //var x=_root.divmove.left;
                //var y=_root.divmove.top;
                var x=_root.divDialog.left;
                var y=_root.divDialog.top;
                var w=mx-x;
                var h=my-y-1;

                if(_root.resizetype==1 || _root.resizetype==4 || _root.resizetype==8)
                {
                    var xmax=_root.divDialog.left+_root.divDialog.width;
                    if(xmax-mx<100)
                    {
                        mx=x;
                    }else
                    {
                        x=mx;
                    }
                    w=xmax-mx;
                }
                //yy
                if(_root.resizetype==1 || _root.resizetype==2 || _root.resizetype==5)
                {
                    var ymax=_root.divDialog.top+_root.divDialog.height;
                    if(ymax-my<100)
                    {
                        my=y;
                    }else
                    {
                        y=my;
                    }
                    h=ymax-my-1;
                }
                if(_root.resizetype==5 || _root.resizetype==7)
                {
                    w=_root.divDialog.width;
                }
                if(_root.resizetype==6 || _root.resizetype==8)
                {
                    h=_root.divDialog.height-1;
                }
                if(w<100)w=100;
                if(h<100)h=100;
                //_root.setSize(_root.divmove,w,h);
               // _root.setPosition(_root.divmove,x,y);
                _root.moveTo( x,y);
                _root.resizeTo(w,h-30);
               // _root.divmove.style.display='';
            }
            try{
                _root.omm(e);

            }catch(e){};
        };
        this.win.document.onmouseup=function()
        {
            //_root.divmove.style.display='none';
            _root.divmask.style.cursor="";
            /*
            if(_root.isMove==true)
            {
                _root.isMove=false;
                _root.moveTo( _root.divmove.left, _root.divmove.top);
            }else if(_root.resizetype>0)
            {
                _root.resizetype=0;
                _root.moveTo( _root.divmove.left, _root.divmove.top);
                _root.resizeTo(_root.divmove.width,_root.divmove.height-30);
            }
            */
            _root.isMove=false;
            _root.resizetype=0;
            try{
                _root.omu();
            }catch(e){};
        };
        this.win.window.onresize=function(e)
        {
            _root.setSize(_root.divmask,0,0);
            var sizep=_root.getSizePage();
            var sizeps=_root.getPageSizeWithScroll();
            if(sizeps.y>sizep.h)
            {
                sizeps.x-=17;
            }
            _root.setSize(_root.divmask,sizeps.x,sizeps.y);
            try{
                _root.wors(e);
            }catch(e){}
        };
        this.win.window.onscroll=function(e)
        {
            var coord=_root.GetCoords();
            var sizep=_root.getSizePage();
            _root.moveTo(coord.x+sizep.w/2-_root.width/2,coord.y+sizep.h/2-_root.height/2-50);
            _root.setSize(_root.divmask,0,0);
            sizep=_root.getSizePage();
            sizeps=_root.getPageSizeWithScroll();
            if(sizeps.y>sizep.h)sizeps.x-=17;
            _root.setSize(_root.divmask,sizeps.x,sizeps.y);
            try{
                _root.wosc(e);
            }catch(e){}
        }
    };
    this.close=function(arg)
    {
        this.divall.style.display='none';
        this.win.document.body.className='';
        try{
            this.win.document.onmousemove=this.omm;
            this.win.document.onmouseup=this.omu;
            this.win.window.onresize=this.wors;
            this.win.window.onscroll=this.wosc;
        }catch(e){}
        if(arg==undefined)
        {
            this.win.document.body.removeChild(this.divall);
        }
    };
    /*
    function: this.this.getSizePage()
    description: get size off window;
    */
    this.getSizePage=function ()
    {
        var mysize=new Object;
        if(this.win.innerHeight) {
            // all except explorer
            mysize.w = this.win.innerWidth;
            mysize.h = this.win.innerHeight;
        } else if(this.win.document.documentElement && this.win.document.documentElement.clientHeight) {
            // explorer 6 strict mode
            mysize.w = this.win.document.documentElement.clientWidth;
            mysize.h = this.win.document.documentElement.clientHeight;
        } else if(document.body) {
            // other explorers
            mysize.w = this.win.document.body.clientWidth;
            mysize.h = this.win.document.body.clientHeight;
};
        return mysize;
    };
    /*
    function: this.this.getPageSizeWithScroll()
    description: get size page has scroll;
    */
    this.getPageSizeWithScroll=function (){
        var mysize=new Object;
    	if (this.win.window.innerHeight && this.win.window.scrollMaxY) {// Firefox
    		mysize.y = this.win.window.innerHeight + this.win.window.scrollMaxY;
    		mysize.x = this.win.window.innerWidth + this.win.window.scrollMaxX;
    	} else if (this.win.document.body.scrollHeight > this.win.document.body.offsetHeight){ // all but Explorer Mac
    		mysize.y = this.win.document.body.scrollHeight;
    		mysize.x = this.win.document.body.scrollWidth;
    	} else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari
    		mysize.y = this.win.document.body.offsetHeight;
    		mysize.x = this.win.document.body.offsetWidth;
      	}
    	return mysize;
    };
    /*
    function: this.this.GetCoords()
    description: x,y of scroll;
    */
    this.GetCoords=function()
    {
        var mysize=new Object;

        if (this.win.document.all)
        {
            if (this.win.document.documentElement.scrollLeft>0)
                mysize.x = this.win.document.body.scrollLeft;
            else
                mysize.x = this.win.document.documentElement.scrollLeft;

            if (!this.win.document.documentElement.scrollTop>0)
                mysize.y = this.win.document.body.scrollTop;
            else
                mysize.y = this.win.document.documentElement.scrollTop;
        }
        else
        {
            mysize.x = this.win.window.pageXOffset;
            mysize.y = this.win.window.pageYOffset;
        }
        return mysize;
    };
}